
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import * as echarts from 'echarts';
import {listNotice} from "@/api/system/notice.js";
import {LoadAllParams} from "@/api/LoadAllParam.js";

// 定义模板引用
const chart1Ref = ref(null);
const chart5Ref = ref(null);
const chartRefs = ref({});

// 图表数据
const charts = ref([
  { id: 1, title: '销售趋势', instance: null },
  { id: 2, title: '产品占比', instance: null },
  { id: 3, title: '地区分布', instance: null },
  { id: 4, title: '月度对比', instance: null },
  { id: 5, title: '用户分布', instance: null }
]);

// 初始化所有图表
const initCharts = () => {
  if (!chart1Ref.value) return;

  // 图表1 - 折线图
  charts.value[0].instance = echarts.init(chart1Ref.value);
  charts.value[0].instance.setOption({
    title: { text: '销售趋势', left: 'center', textStyle: { fontSize: 12 } },
    tooltip: { trigger: 'axis' },
    xAxis: { type: 'category', data: ['1月', '2月', '3月', '4月', '5月', '6月'] },
    yAxis: { type: 'value' },
    series: [
      { name: '销售额', type: 'line', data: [150, 230, 224, 218, 135, 260], smooth: true }
    ],
    grid: { top: 40, bottom: 20, left: 40, right: 20 }
  });

  // 图表2-4 - 使用动态refs
  [2,3,4].forEach(i => {
    if (!chartRefs.value[`chart${i}`]) return;

    charts.value[i-1].instance = echarts.init(chartRefs.value[`chart${i}`]);
    // 为每个图表设置不同的示例配置
    const option = getChartOption(i);
    charts.value[i-1].instance.setOption(option);
  });

  // 图表5 - 雷达图
  if (chart5Ref.value) {
    charts.value[4].instance = echarts.init(chart5Ref.value);
    charts.value[4].instance.setOption({
      title: { text: '用户分布', left: 'center', textStyle: { fontSize: 12 } },
      tooltip: { trigger: 'item' },
      radar: {
        indicator: [
          { name: '18-24岁', max: 100 },
          { name: '25-30岁', max: 100 },
          { name: '31-40岁', max: 100 },
          { name: '41-50岁', max: 100 },
          { name: '50岁以上', max: 100 }
        ]
      },
      series: [
        {
          name: '用户分布',
          type: 'radar',
          data: [
            { value: [90, 50, 86, 30, 15], name: '男性' },
            { value: [70, 75, 60, 50, 20], name: '女性' }
          ],
          areaStyle: { opacity: 0.3 }
        }
      ]
    });
  }
};

// 获取不同图表的配置
const getChartOption = (index) => {
  const options = [
    null, // 0
    null, // 1
    { // 2 - 饼图
      title: { text: '产品占比', left: 'center', textStyle: { fontSize: 12 } },
      tooltip: { trigger: 'item' },
      series: [{
        name: '产品占比',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 1048, name: '产品A' },
          { value: 735, name: '产品B' },
          { value: 580, name: '产品C' },
          { value: 484, name: '产品D' }
        ]
      }]
    },
    { // 3 - 柱状图
      title: { text: '地区分布', left: 'center', textStyle: { fontSize: 12 } },
      tooltip: { trigger: 'axis' },
      xAxis: { type: 'category', data: ['华东', '华北', '华南', '华中', '西南'] },
      yAxis: { type: 'value' },
      series: [{
        name: '销售额',
        type: 'bar',
        data: [120, 200, 150, 80, 70],
        itemStyle: { color: '#67C23A' }
      }],
      grid: { top: 40, bottom: 20, left: 40, right: 20 }
    },
    { // 4 - 双轴图
      title: { text: '月度对比', left: 'center', textStyle: { fontSize: 12 } },
      tooltip: { trigger: 'axis' },
      legend: { data: ['销售额', '增长率'], top: 30 },
      xAxis: { type: 'category', data: ['1月', '2月', '3月', '4月', '5月', '6月'] },
      yAxis: [
        { type: 'value', name: '销售额' },
        { type: 'value', name: '增长率(%)' }
      ],
      series: [
        { name: '销售额', type: 'bar', data: [150, 230, 224, 218, 135, 260] },
        { name: '增长率', type: 'line', yAxisIndex: 1, data: [5, 20, 36, 10, 10, 20] }
      ],
      grid: { top: 70, bottom: 20, left: 50, right: 50 }
    }
  ];
  return options[index];
};

// 响应式调整图表大小
const resizeCharts = () => {
  charts.value.forEach(chart => {
    if (chart.instance) {
      chart.instance.resize();
    }
  });
};

// 调整大小功能
const startResize = (e, chartRefName) => {
  e.preventDefault();
  const chartEl = chartRefs.value[chartRefName] ||
      (chartRefName === 'chart1Ref' ? chart1Ref.value :
          chartRefName === 'chart5Ref' ? chart5Ref.value : null);

  if (!chartEl) return;

  const container = chartEl.parentElement.parentElement;
  const startX = e.clientX;
  const startY = e.clientY;
  const startWidth = parseInt(document.defaultView.getComputedStyle(container).width, 10);
  const startHeight = parseInt(document.defaultView.getComputedStyle(container).height, 10);

  const doDrag = (e) => {
    container.style.width = (startWidth + e.clientX - startX) + 'px';
    container.style.height = (startHeight + e.clientY - startY) + 'px';
  };

  const stopDrag = () => {
    document.documentElement.removeEventListener('mousemove', doDrag, false);
    document.documentElement.removeEventListener('mouseup', stopDrag, false);
    resizeCharts();
  };

  document.documentElement.addEventListener('mousemove', doDrag, false);
  document.documentElement.addEventListener('mouseup', stopDrag, false);
};

function initNotice() {
  listNotice(LoadAllParams).then(res =>{
    notices.value = res.rows
  })
}
// 生命周期钩子
onMounted(() => {
  initCharts();
  initNotice();
  window.addEventListener('resize', resizeCharts);
});

onBeforeUnmount(() => {
  charts.value.forEach(chart => {
    if (chart.instance) {
      chart.instance.dispose();
    }
  });
  window.removeEventListener('resize', resizeCharts);
});

// 公告类型过滤器
const noticeTypeFilter = (type) => {
  const types = { 1: '通知', 2: '公告' };
  return types[type] || '通知';
};

// 公告数据
const notices = ref([
]);

const currentNotice = ref({
  noticeTitle: '',
  noticeContent: '',
  noticeType: '',
  createTime: '',
  createBy: ''
});

const noticeDialogVisible = ref(false);

// 获取公告类型样式
const getNoticeTypeClass = (type) => {
  return type === '2' ? 'warning' : '';
};

// 显示公告详情
const showNoticeDetail = (notice) => {
  currentNotice.value = { ...notice };
  notice.isRead = true;
  noticeDialogVisible.value = true;
};

</script>

<template>
  <div class="dashboard-container">
    <div class="dashboard-title">
      <el-card  class="dashboard-top" shadow="hover">
        <el-row>
          <el-col :span="6">
            <el-statistic title="总人数" :value="268500" />
          </el-col>
          <el-col :span="6">
            <el-statistic :value="138">
              <template #title>
                <div style="display: inline-flex; align-items: center">
                  男女比例
                </div>
              </template>
              <template #suffix>/101</template>
            </el-statistic>
          </el-col>
          <el-col :span="6">
            <el-statistic title="管理员" :value="7" />
          </el-col>
          <el-col :span="6">
            <el-statistic title="反馈通知" :value="562">
              <template #suffix>
                <el-icon style="vertical-align: -0.125em">
                  <ChatLineRound />
                </el-icon>
              </template>
            </el-statistic>
          </el-col>
        </el-row>
      </el-card>

    </div>

    <el-row class="dashboard-content" :gutter="20">
      <!-- 左侧图表区域 -->
      <el-col :span="16">
        <!-- 上方横向图表 -->
        <div class="chart-row">
          <el-card class="chart-container" shadow="hover">
            <div class="chart-wrapper" ref="chart1Ref"></div>
            <div class="chart-title">销售趋势</div>
            <div class="resize-handle" @mousedown="startResize($event, 'chart1Ref')"></div>
          </el-card>
        </div>

        <!-- 下方2x2图表 -->
        <el-row :gutter="20">
          <el-col style="margin-top: 15px" :span="12" v-for="(chart, index) in charts.slice(1, 5)" :key="chart.id">
            <el-card  class="chart-container" shadow="hover">
              <div class="chart-wrapper" :ref="el => { if (el) chartRefs['chart' + (index + 2)] = el }"></div>
              <div class="chart-title">{{ chart.title }}</div>
              <div class="resize-handle" @mousedown="startResize($event, 'chart' + (index + 2))"></div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>

      <!-- 右侧纵向图表 -->
      <el-col :span="8">
        <el-card class="notice-container vertical" shadow="hover">
          <div class="notice-header">
            <span class="notice-title">系统公告</span>
          </div>
          <div class="notice-list" ref="noticeContainerRef">
            <div
                v-for="(notice, index) in notices"
                :key="notice.noticeId"
                class="notice-item"
                @click="showNoticeDetail(notice)"
            >
              <div class="notice-item-title">
                <span class="notice-type-tag" :class="getNoticeTypeClass(notice.noticeType)">
                  {{  noticeTypeFilter(notice.noticeType) }}
                </span>
                {{ notice.noticeTitle }}
              </div>
              <div class="notice-item-time">{{ notice.createTime }}</div>
            </div>
            <div v-if="notices.length === 0" class="no-notice">
              暂无公告
            </div>
          </div>
          <div class="resize-handle" @mousedown="startResize($event, 'noticeContainerRef')"></div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 公告详情弹窗 -->
    <el-dialog
        :title="currentNotice.noticeTitle"
        v-model="noticeDialogVisible"
        width="50%"
        custom-class="notice-dialog"
    >
      <div class="notice-detail-header">
        <el-tag :type="getNoticeTypeClass(currentNotice.noticeType)" size="small">
          {{ noticeTypeFilter(currentNotice.noticeType) }}
        </el-tag>
        <span class="notice-time">{{ currentNotice.createTime }}</span>
        <span class="notice-creator">发布人：{{ currentNotice.createBy }}</span>
      </div>
      <div class="notice-content ql-editor" v-html="currentNotice.noticeContent"></div>
      <template #footer>
        <el-button type="primary" @click="noticeDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
/* 保持原有样式不变 */
.dashboard-container {
  background-color: #f5f5f5;
  padding: 20px;
  border-radius: 4px;
  max-width: 1200px;
  margin: 0 auto;
}
.dashboard-top{
  margin: 0 -9px;
}
.dashboard-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #333;
}

.dashboard-content {
  background-color: white;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.chart-row {
  margin-bottom: 20px;
}

.chart-container {
  position: relative;
  height: 100%;
  border: 1px dashed #e0e0e0;
  background-color: #f9f9f9;
}

.chart-container.vertical {
  height: 420px;
}

.chart-wrapper {
  width: 100%;
  height: calc(100% - 30px);
  min-height: 200px;
}

.chart-title {
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-size: 14px;
  color: #666;
  border-top: 1px solid #eee;
}

.resize-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background-color: #409eff;
  border-radius: 50%;
  right: 4px;
  bottom: 4px;
  cursor: nwse-resize;
  z-index: 10;
}

@media (max-width: 768px) {
  .el-col {
    width: 100%;
  }

  .chart-container.vertical {
    height: 200px;
  }
}


/* 公告区域专属样式 */
.notice-container {
  position: relative;
  border: 1px dashed #e0e0e0;
  background-color: #f9f9f9;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.notice-header {
  padding: 10px 15px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.notice-title {
  font-weight: bold;
  color: #333;
  text-align: left;
}

.notice-list {
  flex: 1;
  padding: 0 10px;
  height: 100%;
}

.notice-item {
  padding: 12px 10px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.3s;
  text-align: left;
}

.notice-item:hover {
  background-color: #f5f7fa;
}


.notice-type-tag {
  display: inline-block;
  padding: 0 5px;
  margin-right: 8px;
  font-size: 12px;
  border-radius: 3px;
  background-color: #409EFF;
  color: white;
}

.notice-type-tag.warning {
  background-color: #E6A23C;
}

.notice-item-time {
  font-size: 12px;
  color: #999;
}

.no-notice {
  text-align: center;
  color: #999;
  padding: 50px 0;
}

/* 公告详情弹窗样式 */
.notice-detail-header {
  margin-bottom: 15px;
  display: flex;
  align-items: center;
}

.notice-time {
  margin: 0 15px;
  color: #666;
  font-size: 13px;
}

.notice-creator {
  color: #666;
  font-size: 13px;
}

.notice-content {
  min-height: 300px;
  padding: 0 10px;
}

/* 保持原有的resize-handle样式 */
.resize-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background-color: #409eff;
  border-radius: 50%;
  right: 4px;
  bottom: 4px;
  cursor: nwse-resize;
  z-index: 10;
}
.el-col {
  text-align: center;
}
</style>
